package com.yunhe.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.GoodsType;
import com.yunhe.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
@RestController
@RequestMapping("/goodstype")
@Api(value = "/goodstype",description = "物品类型管理")
public class GoodsTypeController {
    @Autowired
    GoodsTypeService goodsTypeService;

    @GetMapping("/list")
    @ApiOperation("获取所有的分类列表")
    public Result list(){
        return Result.success(goodsTypeService.list());
    }

    @PostMapping("/listPage")
    @ApiOperation("根据类品进行分页和模糊查询")
    public Result listPage(
            @ApiParam("类品名 页码 每页显示条数")
            @RequestBody QueryPageParam query){
        //获取响应的参数：
        HashMap param=query.getParam("param");
        String name= (String) param.get("name");

        //分页对象;设置额前端页面传递过来的页码和每页显示的条数；
        Page<GoodsType> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        //包装器查询
        LambdaQueryWrapper<GoodsType> queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name))
            queryWrapper.like(GoodsType::getName, name);
        //查询：利用业务层接口对象查询;
        Page<GoodsType> result = goodsTypeService.page(page, queryWrapper);

        return Result.success(result.getRecords(), result.getTotal());
    }

    @PostMapping("/save")
    @ApiOperation("保存物品类别信息")
    public Result save(@RequestBody GoodsType goodsType){
        return goodsTypeService.save(goodsType)?Result.success():Result.fail();
    }

    @PutMapping("/update")
    @ApiOperation("修改物品类别信息")
    public Result update(@RequestBody GoodsType goodsType){
        return goodsTypeService.updateById(goodsType)?Result.success():Result.fail();
    }

    /**
     * 作业：写成Restful风格的删除？
     * /del/{id}
     * PathVariable String id
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("根据id物品类别信息")
    @ApiImplicitParam(paramType = "query",dataType = "Long",required = true,name = "id",value = "物品编号")
    public Result del(String id){
        return goodsTypeService.removeById(id)?Result.success():Result.fail();
    }
}